// eslint-disable-next-line import/namespace
import Cookie from 'js-cookie'
import { Context } from '@nuxt/types'
interface IData {
  [key: string]: any
}
export class CustomCookie {
  /**
   * 描述 设置 cookie
   * @date 2022-03-25
   * @param {any} key:string
   * @param {any} value:string
   * @param {any} config={}
   * @returns {any}
   */
  static setCookie(key: string, value: string, config = {}) {
    Cookie.set(key, value, config)
  }

  /**
   * 描述 获取 cookie
   * @date 2022-03-25
   * @param {any} key:string
   * @returns {any}
   */
  static getCookie(key: string) {
    return Cookie.get(key)
  }

  /**
   * 描述 删除 cookie
   * @date 2022-03-25
   * @param {any} key:string
   * @param {any} config={}
   * @returns {any}
   */
  static removeCookie(key: string, config = {}) {
    return Cookie.remove(key, config)
  }

  /**
   * 描述 获取服务器的 cookie
   * @date 2022-03-25
   * @param {any} {req}:Context
   * @returns {any}
   */
  static getcookiesInServer(req: any) {
    // eslint-disable-next-line camelcase
    const serviceCookie: IData = {}
    req &&
      req.headers.cookie &&
      req.headers.cookie.split(';').forEach(function (val: any) {
        const parts = val.split('=')
        // eslint-disable-next-line camelcase
        serviceCookie[parts[0].trim()] = (parts[1] || '').trim()
      })
    // eslint-disable-next-line camelcase
    return serviceCookie
  }
}
